package com.zz.framework.core.base

import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.zz.framework.R
import com.zz.framework.core.adapter.FragmentAdapter

/**
 * Copyright © 2020 zanezhao All rights reserved.
 * Created on 2020/3/20 11:24 AM.
 * @version: V1.0
 */
abstract class BaseTabFragment: BaseLazyFragment() {
    override fun onInitAttribute(ba: BaseAttribute) {
        super.onInitAttribute(ba)
        ba.mContentLayoutId= R.layout.fragment_tab_layout
    }
    var viewPager:ViewPager? = null
    var tabLayout:TabLayout? = null

    protected var titles: MutableList<String> = mutableListOf()
    protected var fragments: MutableList<Fragment> = mutableListOf()

    override fun initView(view: View) {
        super.initView(view)
        tabLayout = view.findViewById(R.id.tabLayout)
        viewPager = view.findViewById(R.id.viewPager)
        titles.clear()
        fragments.clear()
        initFragment()
        initViewPager()
    }

    protected fun initViewPager() {
        viewPager?.setAdapter(FragmentAdapter(childFragmentManager, fragments, titles))
        viewPager?.setOffscreenPageLimit(fragments.size)
        viewPager?.setOffscreenPageLimit(titles.size)
        viewPager?.setAnimationCacheEnabled(false)
        tabLayout?.setupWithViewPager(viewPager)
    }

    abstract fun initFragment()

}